import sys

from PyQt6.QtCore import Qt
from PyQt6.QtGui import QIcon
from PyQt6.QtWidgets import QApplication, QFrame, QHBoxLayout
from qfluentwidgets import MSFluentWindow, setFont, SubtitleLabel, NavigationItemPosition, NavigationAvatarWidget, \
	isDarkTheme
from qfluentwidgets import FluentIcon as FIF

from src.control.control import Control
from src.setting.config import cfg
from src.component.DeviceInterface import DeviceInterface
from src.component.DataInterface import DataInterface
from src.component.SettingInterface import SettingInterface
from src.component.InspectionInterface import InspectionInterface
from src.component.MotionControllerInterface import MotionControllerInterface

#之后会删
class Widget(QFrame):

    def __init__(self, text: str, parent=None):
        super().__init__(parent=parent)
        self.label = SubtitleLabel(text, self)
        self.hBoxLayout = QHBoxLayout(self)

        setFont(self.label, 24)
        self.label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        self.hBoxLayout.addWidget(self.label, 1, Qt.AlignmentFlag.AlignCenter)
        self.setObjectName(text.replace(' ', '-'))

class MainWindow(MSFluentWindow):
	def __init__(self):
		super().__init__()
		self.control = Control()

		#声明每一个子页面
		self.homeInterface = Widget('Home Interface', self)
		self.deviceInterface = DeviceInterface('Device Interface',self.control, self)
		self.motionInterface = MotionControllerInterface('Motion Interface',self.control)
		self.inspectionInterface = InspectionInterface('Inspection Interface', self.control,self)
		self.dataInterface = DataInterface('Data Interface',self)
		self.settingInterface = SettingInterface('Setting Interface',self)
		self.initNavigation()
		self.initWindow()

	def initNavigation(self):
		self.addSubInterface(self.homeInterface, FIF.HOME, '主页', FIF.HOME_FILL)
		self.addSubInterface(self.deviceInterface, FIF.APPLICATION, '设备')
		self.addSubInterface(self.motionInterface, FIF.MOVE, '运动')
		self.addSubInterface(self.inspectionInterface,FIF.SPEED_HIGH,'检测')
		self.addSubInterface(self.dataInterface,FIF.FOLDER,'数据')

		self.navigationInterface.addWidget(
			routeKey='avatar',
			widget=NavigationAvatarWidget('Avatar', 'resources/image/Avatar.png'),
			#onClick=self.showMessageBox,
			#selectable=False,
			position=NavigationItemPosition.BOTTOM,
		)
		self.addSubInterface(self.settingInterface, FIF.SETTING, '设置', position=NavigationItemPosition.BOTTOM)
		self.navigationInterface.setCurrentItem(self.homeInterface.objectName())

	# 	self.setQss()
	# 	cfg.themeChanged.connect(self.setQss)
	#
	# def setQss(self):
	# 	theme = 'dark' if isDarkTheme() else 'light'
	# 	with open(f'./resources/qss/{theme}/demo.qss', encoding='utf-8') as f:
	# 		self.setStyleSheet(f.read())
	def initWindow(self):
		self.resize(900, 650)
		self.setWindowIcon(QIcon("./resources/icons/logo.png"))
		self.setWindowTitle("光谱仪控制平台")

		desktop = QApplication.screens()[0].availableGeometry()
		w, h = desktop.width(), desktop.height()
		self.move(w // 2 - self.width() // 2, h // 2 - self.height() // 2)

if __name__ == "__main__":
	app = QApplication(sys.argv)
	w = MainWindow()
	w.show()
	sys.exit(app.exec())
